package com.bsy.videostation.adapter

import android.view.LayoutInflater
import android.widget.ImageView
import androidx.recyclerview.widget.RecyclerView
import coil.load
import coil.transform.RoundedCornersTransformation
import com.bsy.framework.ext.data
import com.bsy.framework.utils.dpToPx
import com.bsy.videostation.constant.appPic
import com.bsy.videostation.model.res.AppSourceItem
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.dg.videostation.R

/**
 * Created by since on 2024/5/28.
 * Copyright
 * 描述：
 */
class AllMainAdapter : BaseQuickAdapter<AppSourceItem, BaseViewHolder>(R.layout.item_all_main) {
    override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
        super.onAttachedToRecyclerView(recyclerView)
        val emptyView = LayoutInflater.from(context).inflate(R.layout.list_empty, recyclerView, false)
        setEmptyView(emptyView)
    }

    override fun convert(holder: BaseViewHolder, item: AppSourceItem) {
        val randomIndex = appPic.indices.random()
        val randomCover = appPic[randomIndex]
        val cover = if (item.appIcon.isNullOrEmpty()) randomCover else item.appIcon
        holder.getView<ImageView>(R.id.iv_cover).load(cover) {
            error(R.drawable.no_app_img)
            transformations(RoundedCornersTransformation(dpToPx(10F)))
        }
        holder.setText(R.id.tv_title, if (item.appName.isNullOrBlank()) "виде" else item.appName.data())
    }
}